package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.bean.Yhxddx;
import java.util.Set;
import com.sandking.db.dao.YhxddxDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhxddxJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhxddx getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhxddx yhxddx = null;
		try{
			String key = "Yhxddx_Object";
			String field = SK_Plus.b("id:",id).e();
			yhxddx = Yhxddx.createForJson(jedis.hget(key,field));
			if(yhxddx == null){
				yhxddx = YhxddxDao.getById(id);
				if(yhxddx!=null){
					loadCache(yhxddx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhxddx;
	}
	
	/**
	 * 根据( 用户id ) 查询
	 */
	public static List<Yhxddx> getByYhid(int yhid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
		try{
			String key = "Yhxddx_Index";
			String field = SK_Plus.b(key,"yhid:",yhid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxddx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxddxs = Yhxddx.createForJson(jsons);
			}else{
				yhxddxs = YhxddxDao.getByYhid(yhid);
				if(yhxddxs!=null && !yhxddxs.isEmpty()){
					loadCaches(yhxddxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxddxs;
	}
	
	/**
	 * 根据( 目标用户id ) 查询
	 */
	public static List<Yhxddx> getByMbyhid(int mbyhid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
		try{
			String key = "Yhxddx_Index";
			String field = SK_Plus.b(key,"mbyhid:",mbyhid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxddx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxddxs = Yhxddx.createForJson(jsons);
			}else{
				yhxddxs = YhxddxDao.getByMbyhid(mbyhid);
				if(yhxddxs!=null && !yhxddxs.isEmpty()){
					loadCaches(yhxddxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxddxs;
	}
	
	/**
	 * 根据( 用户id  有效 ) 查询
	 */
	public static List<Yhxddx> getByYhidYx(int yhid, boolean yx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
		try{
			String key = "Yhxddx_Index";
			String field = SK_Plus.b(key,"yhid, yx:",yhid, yx).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxddx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxddxs = Yhxddx.createForJson(jsons);
			}else{
				yhxddxs = YhxddxDao.getByYhidYx(yhid, yx);
				if(yhxddxs!=null && !yhxddxs.isEmpty()){
					loadCaches(yhxddxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxddxs;
	}
	

	public static void loadCache(Yhxddx yhxddx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhxddx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhxddx yhxddx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxddx_Object";
		String field = SK_Plus.b("id:",yhxddx.getId()).e();
		String data = yhxddx.toJson();
		p.hset(key,field,data);
		
		String primaryKey = String.valueOf(yhxddx.getId());
		key = "Yhxddx_Index";
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yhid:",yhxddx.getYhid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"mbyhid:",yhxddx.getMbyhid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"yhid, yx:",yhxddx.getYhid(),yhxddx.getYx()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhxddx> yhxddxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxddx yhxddx : yhxddxs){
				loadCache(yhxddx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhxddx yhxddx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhxddx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhxddx yhxddx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxddx_Object";
		String field = SK_Plus.b("id:",yhxddx.getId()).e();
		p.hdel(key,field);
		
		key = "Yhxddx_Index";
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhxddx.getId());
		field = SK_Plus.b(key,"yhid:",yhxddx.getYhid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"mbyhid:",yhxddx.getMbyhid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"yhid, yx:",yhxddx.getYhid(),yhxddx.getYx()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhxddx> yhxddxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxddx yhxddx : yhxddxs){
				clearCache(yhxddx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhxddx insert(Yhxddx yhxddx){
		yhxddx = YhxddxDao.insert(yhxddx);
		if(yhxddx!=null){
			loadCache(yhxddx);
		}
    	return yhxddx;
    }
    
    public static Yhxddx update(Yhxddx yhxddx){
    	yhxddx = YhxddxDao.update(yhxddx);
    	if(yhxddx!=null){
			loadCache(yhxddx);
		}
    	return yhxddx;
    }
    
    public static boolean delete(Yhxddx yhxddx){
    	boolean bool = YhxddxDao.delete(yhxddx);
    	if(bool){
    		clearCache(yhxddx);
    	}
    	return bool;
    }
}